package net.openl10n.trxml;
import java.io.IOException;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;

import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.Location;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

import org.codehaus.stax2.AttributeInfo;
import org.codehaus.stax2.DTDInfo;
import org.codehaus.stax2.LocationInfo;
import org.codehaus.stax2.XMLStreamReader2;
import org.codehaus.stax2.typed.Base64Variant;
import org.codehaus.stax2.typed.TypedArrayDecoder;
import org.codehaus.stax2.typed.TypedValueDecoder;
import org.codehaus.stax2.validation.ValidationProblemHandler;
import org.codehaus.stax2.validation.XMLValidationSchema;
import org.codehaus.stax2.validation.XMLValidator;


public class TrXMLStreamReaderImpl implements TrXMLStreamReader{

	private final XMLStreamReader2 reader;
	private final Set<String> translatables = new HashSet<String>();
	
	public TrXMLStreamReaderImpl(XMLStreamReader2 reader) {
		this.reader = reader;
	}

	@Override
	public void addTranslatableElement(String elementName) {
		if(elementName != null && !elementName.isEmpty())
			translatables.add(elementName);
	}
	
	@Override
	public int getTranslatableType() {
		switch(reader.getEventType()){
		case START_ELEMENT:
			return translatables.contains(reader.getName().getLocalPart()) ?
					CONTEXT_TRANSLATABLE_BLOCK : CONTENT_NOT_TRANSLATABLE;
		default:
			return TrXMLStreamReader.CONTENT_NOT_TRANSLATABLE;				
		}
		
	}
	
	// The rest is simply calling XMLStreamReader methods
	
	@Override
	public void close() throws XMLStreamException {
		reader.close();
	}

	@Override
	public int getAttributeCount() {
		return reader.getAttributeCount();
	}

	@Override
	public String getAttributeLocalName(int index) {
		return reader.getAttributeLocalName(index);
	}

	@Override
	public QName getAttributeName(int index) {
		return reader.getAttributeName(index);
	}

	@Override
	public String getAttributeNamespace(int index) {
		return reader.getAttributeNamespace(index);
	}

	@Override
	public String getAttributePrefix(int index) {
		return reader.getAttributePrefix(index);
	}

	@Override
	public String getAttributeType(int index) {
		return reader.getAttributeType(index);
	}

	@Override
	public String getAttributeValue(int index) {
		return reader.getAttributeValue(index);
	}

	@Override
	public String getAttributeValue(String namespaceURI, String localName) {
		return reader.getAttributeValue(namespaceURI, localName);
	}

	@Override
	public String getCharacterEncodingScheme() {
		return reader.getCharacterEncodingScheme();
	}

	@Override
	public String getElementText() throws XMLStreamException {
		return reader.getElementText();
	}

	@Override
	public String getEncoding() {
		return reader.getEncoding();
	}

	@Override
	public int getEventType() {
		return reader.getEventType();
	}

	@Override
	public String getLocalName() {
		return reader.getLocalName();
	}

	@Override
	public Location getLocation() {
		return reader.getLocation();
	}

	@Override
	public QName getName() {
		return reader.getName();
	}

	@Override
	public NamespaceContext getNamespaceContext() {
		return reader.getNamespaceContext();
	}

	@Override
	public int getNamespaceCount() {
		return reader.getNamespaceCount();
	}

	@Override
	public String getNamespacePrefix(int index) {
		return reader.getNamespacePrefix(index);
	}

	@Override
	public String getNamespaceURI() {
		return reader.getNamespaceURI();
	}

	@Override
	public String getNamespaceURI(String prefix) {
		return reader.getNamespaceURI(prefix);
	}

	@Override
	public String getNamespaceURI(int index) {
		return reader.getNamespaceURI(index);
	}

	@Override
	public String getPIData() {
		return reader.getPIData();
	}

	@Override
	public String getPITarget() {
		return reader.getPITarget();
	}

	@Override
	public String getPrefix() {
		return reader.getPrefix();
	}

	@Override
	public Object getProperty(String name) throws IllegalArgumentException {
		return reader.getProperty(name);
	}

	@Override
	public String getText() {
		return reader.getText();
	}

	@Override
	public char[] getTextCharacters() {
		return reader.getTextCharacters();
	}

	@Override
	public int getTextCharacters(int sourceStart, char[] target,
			int targetStart, int length) throws XMLStreamException {
		return reader.getTextCharacters(sourceStart, target, targetStart, length);
	}

	@Override
	public int getTextLength() {
		return reader.getTextLength();
	}

	@Override
	public int getTextStart() {
		return reader.getTextStart();
	}

	@Override
	public String getVersion() {
		return reader.getVersion();
	}

	@Override
	public boolean hasName() {
		return reader.hasName();
	}

	@Override
	public boolean hasNext() throws XMLStreamException {
		return reader.hasNext();
	}

	@Override
	public boolean hasText() {
		return reader.hasText();
	}

	@Override
	public boolean isAttributeSpecified(int index) {
		return reader.isAttributeSpecified(index);
	}

	@Override
	public boolean isCharacters() {
		return reader.isCharacters();
	}

	@Override
	public boolean isEndElement() {
		return reader.isEndElement();
	}

	@Override
	public boolean isStandalone() {
		return reader.isStandalone();
	}

	@Override
	public boolean isStartElement() {
		return reader.isStartElement();
	}

	@Override
	public boolean isWhiteSpace() {
		return reader.isWhiteSpace();
	}

	@Override
	public int next() throws XMLStreamException {
		return reader.next();
	}

	@Override
	public int nextTag() throws XMLStreamException {
		return reader.nextTag();
	}

	@Override
	public void require(int type, String namespaceURI, String localName)
			throws XMLStreamException {
		reader.require(type, namespaceURI, localName);
		
	}

	@Override
	public boolean standaloneSet() {
		return reader.standaloneSet();
	}

	@Override
	public void closeCompletely() throws XMLStreamException {
		reader.closeCompletely();
		
	}

	@Override
	public AttributeInfo getAttributeInfo() throws XMLStreamException {
		return reader.getAttributeInfo();
	}

	@Override
	public DTDInfo getDTDInfo() throws XMLStreamException {
		return reader.getDTDInfo();
	}

	@Override
	public int getDepth() {
		return reader.getDepth();
	}

	@Override
	@Deprecated
	public Object getFeature(String name) {
		return reader.getFeature(name);
	}

	@Override
	public LocationInfo getLocationInfo() {
		return reader.getLocationInfo();
	}

	@Override
	public NamespaceContext getNonTransientNamespaceContext() {
		return reader.getNonTransientNamespaceContext();
	}

	@Override
	public String getPrefixedName() {
		return reader.getPrefixedName();
	}

	@Override
	public int getText(Writer w, boolean preserveContents) throws IOException,
			XMLStreamException {
		return reader.getText(w, preserveContents);
	}

	@Override
	public boolean isEmptyElement() throws XMLStreamException {
		return reader.isEmptyElement();
	}

	@Override
	public boolean isPropertySupported(String name) {
		return reader.isPropertySupported(name);
	}

	@Override
	@Deprecated
	public void setFeature(String name, Object value) {
		reader.setFeature(name, value);
	}

	@Override
	public boolean setProperty(String name, Object value) {
		return reader.setProperty(name, value);
	}

	@Override
	public void skipElement() throws XMLStreamException {
		reader.skipElement();
		
	}

	@Override
	public void getAttributeAs(int index, TypedValueDecoder tvd)
			throws XMLStreamException {
		reader.getAttributeAs(index, tvd);
		
	}

	@Override
	public int getAttributeAsArray(int index, TypedArrayDecoder tad)
			throws XMLStreamException {
		return reader.getAttributeAsArray(index, tad);
	}

	@Override
	public byte[] getAttributeAsBinary(int index) throws XMLStreamException {
		return reader.getAttributeAsBinary(index);
	}

	@Override
	public byte[] getAttributeAsBinary(int index, Base64Variant v)
			throws XMLStreamException {
		return reader.getAttributeAsBinary(index, v);
	}

	@Override
	public boolean getAttributeAsBoolean(int index) throws XMLStreamException {
		return reader.getAttributeAsBoolean(index);
	}

	@Override
	public BigDecimal getAttributeAsDecimal(int index)
			throws XMLStreamException {
		return reader.getAttributeAsDecimal(index);
	}

	@Override
	public double getAttributeAsDouble(int index) throws XMLStreamException {
		return reader.getAttributeAsDouble(index);
	}

	@Override
	public double[] getAttributeAsDoubleArray(int index)
			throws XMLStreamException {
		return reader.getAttributeAsDoubleArray(index);
	}

	@Override
	public float getAttributeAsFloat(int index) throws XMLStreamException {
		return reader.getAttributeAsFloat(index);
	}

	@Override
	public float[] getAttributeAsFloatArray(int index)
			throws XMLStreamException {
		return reader.getAttributeAsFloatArray(index);
	}

	@Override
	public int getAttributeAsInt(int index) throws XMLStreamException {
		return reader.getAttributeAsInt(index);
	}

	@Override
	public int[] getAttributeAsIntArray(int index) throws XMLStreamException {
		return reader.getAttributeAsIntArray(index);
	}

	@Override
	public BigInteger getAttributeAsInteger(int index)
			throws XMLStreamException {
		return reader.getAttributeAsInteger(index);
	}

	@Override
	public long getAttributeAsLong(int index) throws XMLStreamException {
		return reader.getAttributeAsLong(index);
	}

	@Override
	public long[] getAttributeAsLongArray(int index) throws XMLStreamException {
		return reader.getAttributeAsLongArray(index);
	}

	@Override
	public QName getAttributeAsQName(int index) throws XMLStreamException {
		return reader.getAttributeAsQName(index);
	}

	@Override
	public int getAttributeIndex(String namespaceURI, String localName) {
		return reader.getAttributeIndex(namespaceURI, localName);
	}

	@Override
	public void getElementAs(TypedValueDecoder tvd) throws XMLStreamException {
		reader.getElementAs(tvd);
		
	}

	@Override
	public byte[] getElementAsBinary() throws XMLStreamException {
		return reader.getElementAsBinary();
	}

	@Override
	public byte[] getElementAsBinary(Base64Variant variant)
			throws XMLStreamException {
		return reader.getElementAsBinary(variant);
	}

	@Override
	public boolean getElementAsBoolean() throws XMLStreamException {
		return reader.getElementAsBoolean();
	}

	@Override
	public BigDecimal getElementAsDecimal() throws XMLStreamException {
		return reader.getElementAsDecimal();
	}

	@Override
	public double getElementAsDouble() throws XMLStreamException {
		return reader.getElementAsDouble();
	}

	@Override
	public float getElementAsFloat() throws XMLStreamException {
		return reader.getElementAsFloat();
	}

	@Override
	public int getElementAsInt() throws XMLStreamException {
		return reader.getElementAsInt();
	}

	@Override
	public BigInteger getElementAsInteger() throws XMLStreamException {
		return reader.getElementAsInteger();
	}

	@Override
	public long getElementAsLong() throws XMLStreamException {
		return reader.getElementAsLong();
	}

	@Override
	public QName getElementAsQName() throws XMLStreamException {
		return reader.getElementAsQName();
	}

	@Override
	public int readElementAsArray(TypedArrayDecoder tad)
			throws XMLStreamException {
		return reader.readElementAsArray(tad);
	}

	@Override
	public int readElementAsBinary(byte[] resultBuffer, int offset,
			int maxLength) throws XMLStreamException {
		return reader.readElementAsBinary(resultBuffer, offset, maxLength);
	}

	@Override
	public int readElementAsBinary(byte[] resultBuffer, int offset,
			int maxLength, Base64Variant variant) throws XMLStreamException {
		return reader.readElementAsBinary(resultBuffer, offset, maxLength, variant);
	}

	@Override
	public int readElementAsDoubleArray(double[] resultBuffer, int offset,
			int length) throws XMLStreamException {
		return reader.readElementAsDoubleArray(resultBuffer, offset, length);
	}

	@Override
	public int readElementAsFloatArray(float[] resultBuffer, int offset,
			int length) throws XMLStreamException {
		return reader.readElementAsFloatArray(resultBuffer, offset, length);
	}

	@Override
	public int readElementAsIntArray(int[] resultBuffer, int offset, int length)
			throws XMLStreamException {
		return reader.readElementAsIntArray(resultBuffer, offset, length);
	}

	@Override
	public int readElementAsLongArray(long[] resultBuffer, int offset,
			int length) throws XMLStreamException {
		return reader.readElementAsLongArray(resultBuffer, offset, length);
	}

	@Override
	public ValidationProblemHandler setValidationProblemHandler(
			ValidationProblemHandler h) {
		return reader.setValidationProblemHandler(h);
	}

	@Override
	public XMLValidator stopValidatingAgainst(XMLValidationSchema schema)
			throws XMLStreamException {
		return reader.stopValidatingAgainst(schema);
	}

	@Override
	public XMLValidator stopValidatingAgainst(XMLValidator validator)
			throws XMLStreamException {
		return reader.stopValidatingAgainst(validator);
	}

	@Override
	public XMLValidator validateAgainst(XMLValidationSchema schema)
			throws XMLStreamException {
		return reader.validateAgainst(schema);
	}

	
}
